<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
            <script type="text/javascript" src="lib/html5shiv.js"></script>
            <script type="text/javascript" src="lib/respond.min.js"></script>
            <![endif]-->
    <link rel="stylesheet" type="text/css" href="../static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="../lib/Hui-iconfont/1.0.8/iconfont.css" />

    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="../static/h-ui.admin/css/style.css" />
    <link rel="stylesheet" href="./../lib/layui/css/layui.css">
    <!--[if IE 6]>
            <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js" ></script>
            <script>DD_belatedPNG.fix('*');</script>
            <![endif]-->
    <title>编辑</title>
</head>

<body>
    <div class="page-container">
        <form action="" method="post" class="form form-horizontal" id="form-article-add">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">标题：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" value="" placeholder="" id="title" name="">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">分类：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="" style="width: 100%" onchange="gradeChange()" class="input-text" id="typeStatus"
                        style="width:15%;margin-left: 1%">
                        <option value="0">资讯</option>
                        <option value="1">视频</option>
                    </select>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-2">状态：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <select name="" style="width: 100%" class="input-text" id="status" style="width:15%;margin-left: 1%">
                        <option value="">-请选择-</option>
                        <option value="0">不显示</option>
                        <option value="1">显示</option>
                    </select>
                </div>
            </div>
            <div class="row cl" id="videoBox" style="display:none">
                <label class="form-label col-xs-4 col-sm-2">视频：</label>
                <div class="layui-upload formControls col-xs-8 col-sm-9">
                    <button type="button" class="layui-btn" id="video"><i class="layui-icon"></i>上传视频</button>
                    <div class="layui-upload-list">
                        <video id="demo9" src="" controls="controls" />
                    </div>
                </div>
            </div>
            <div class="row cl" id="fwb" style="display:none">
                <label class="form-label col-xs-4 col-sm-2">详细内容：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <textarea id="demo" style="display: none;"></textarea>
                </div>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2" style="margin-bottom:20px;">
                    <div onClick="save_submit();" class="btn btn-primary radius">保存</div>
                    <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </form>
    </div>
    <input type="" name="select_vvv" id="select_vvv" value="" style="display:none;" />
    <script type="text/javascript" src="../lib/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="./../static/js/common.js"></script>
    <script type="text/javascript" src="../lib/layer/2.4/layer.js"></script>
    <script type="text/javascript" src="../static/h-ui/js/H-ui.min.js"></script>
    <script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.js"></script>
    <script type="text/javascript" src="./../lib/layui/layui.js"></script>
    <script>
        var parentId = parent.$("#details_id").val();
        function gradeChange() {
            if ($("#typeStatus option:selected").val() == 0) {
                $("#fwb").show();
                $("#videoBox").hide()
            } else if ($("#typeStatus option:selected").val() == 1) {
                $("#videoBox").show();
                $("#fwb").hide();
            }
        }
        var videoUrl = '';
        layui.use(['layedit', 'upload'], function () {
            var layedit = layui.layedit,
                upload = layui.upload;
            var uploadInst = upload.render({
                elem: '#video'
                , url: admin_url + '/uf/uploadFile'
                // , data: {}
                , accept: 'video', //视频
                exts: 'mp4|mov|mtv|rm|rmvb|wmv|amv|dmv|flv',
                before: function () {
                    layer.load();
                }, //视频
                done: function (res) {
                        layer.closeAll('loading');
                    if (res.code != 0) {
                        return layer.msg(res.msg);
                    }
                    if (res.code == 0) {
                        videoUrl = res.data.src;
                        $("#videourl").val(res.data.src);
                        $("#demo9").attr("src", res.data.src);
                        var img = videoUrl;
                    $.ajax({
                    type: "POST", //用post方式传输
                    xhrFields: { withCredentials: true },
                    data: {
                        token: token,
                        tokenUserId: userId,
                        title: $("#title").val(),
                        type: $("#typeStatus option:selected").val(),
                        img: img,
                        status: $("#status option:selected").val(),
                        id: parentId
                    },
                    url: admin_url + "/kvmNews/updNews",
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg('修改成功', {
                                time: 1000
                            })
                        } else {
                            layer.msg(data.msg)
                        }
                    }
                });
        
                    }
                }
                , error: function () {
                        layer.closeAll('loading');
                        layer.msg("上传失败，请重试")
                }
            });
            //构建一个默认的编辑器
            var index = layedit.build('demo', {
                height: 500,
                uploadImage: { url: admin_url + '/uf/uploadFile', type: 'post' }
            });

        })
        $(function () {
            // 获取信息
            $.ajax({
                type: "POST", //用post方式传输
                xhrFields: { withCredentials: true },
                data: { token: token, tokenUserId: userId, id: parentId },
                url: admin_url + "/kvmNews/news",
                success: function (data) {
                    if (data.code == 200) {
                        $("#title").val(data.data.title);
                        $("#typeStatus").val(data.data.type);
                        $("#status").val(data.data.status);
                        $("#title").val(data.data.title);
                        if ($("#typeStatus option:selected").val() == 0) {
                            $('#demo').val(data.data.content)
                            $("#fwb").show();
                            $("#videoBox").hide();
                        } else if ($("#typeStatus option:selected").val() == 1) {
                            $("#demo9").attr('src', data.data.img);
                            $("#videoBox").show();
                            $("#fwb").hide();
                        }
                    }
                }
            });
        })
        // 保存
        function save_submit() {
            if ($("#title").val() == "") {
                layer.msg("请填写标题");
                return;
            } else {
                if ($("#typeStatus option:selected").val() == 0) {
                    var content = $(window.frames["LAY_layedit_1"].document).find('body').html();
                    $.ajax({
                    type: "POST", //用post方式传输
                    xhrFields: { withCredentials: true },
                    data: {
                        token: token,
                        tokenUserId: userId,
                        title: $("#title").val(),
                        type: $("#typeStatus option:selected").val(),
                        content: content,
                        status: $("#status option:selected").val(),
                        id: parentId
                    },
                    url: admin_url + "/kvmNews/updNews",
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg('修改成功', {
                                time: 1000,
                                end: function () {
                                    // window.parent.location.reload();
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                    $(window.parent.document).find('.layui-laypage-btn').click()
                                }
                            })
                        } else {
                            layer.msg(data.msg)
                        }
                    }
                });
                } else if ($("#typeStatus option:selected").val() == 1) {
                    var img = videoUrl;
                    $.ajax({
                    type: "POST", //用post方式传输
                    xhrFields: { withCredentials: true },
                    data: {
                        token: token,
                        tokenUserId: userId,
                        title: $("#title").val(),
                        type: $("#typeStatus option:selected").val(),
                        img: img,
                        status: $("#status option:selected").val(),
                        id: parentId
                    },
                    url: admin_url + "/kvmNews/updNews",
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg('修改成功', {
                                time: 1000,
                                end: function () {
                                    // window.parent.location.reload();
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                    $(window.parent.document).find('.layui-laypage-btn').click()
                                }
                            })
                        } else {
                            layer.msg(data.msg)
                        }
                    }
                });
                }
            }
        }
    </script>
</body>

</html>